<div class="layui-fluid layui-anim febs-anim" id="febs-service-info" lay-title="我的服务">
    <div class="layui-row febs-container">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-tab layui-tab-card">
                    <ul class="layui-tab-title">
                        <li class="layui-this">我提供的</li>
                        <li>我申请的</li>
                    </ul>
                    <div class="layui-tab-content">
                        <div class="layui-tab-item layui-show">
                            <form class="layui-form layui-table-form" lay-filter="service-info-form"
                                  onsubmit="return false">
                                <div class="layui-row">
                                    <div class="layui-col-md10">
                                        <div class="layui-form-item">
                                            <div class="layui-inline">
                                                <label class="layui-form-label layui-form-label-sm">系统名称</label>
                                                <div class="layui-input-inline">
                                                    <input type="text" name="spName" autocomplete="on"
                                                           class="layui-input">
                                                </div>
                                            </div>

                                            <div class="layui-inline">
                                                <label class="layui-form-label layui-form-label-sm">服务名称</label>
                                                <div class="layui-input-inline">
                                                    <input type="text" name="name" autocomplete="on"
                                                           class="layui-input">
                                                </div>
                                            </div>
                                            <div class="layui-inline">
                                                <label class="layui-form-label layui-form-label-sm">状态</label>
                                                <div class="layui-input-inline">
                                                    <select name="status">
                                                        <option value=""></option>
                                                        <option value="0">注册</option>
                                                        <option value="1">审核</option>
                                                        <option value="2">可用</option>
                                                        <option value="3">禁用</option>
                                                    </select>
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                    <div class="layui-col-md2 layui-col-sm12 layui-col-xs12 table-action-area">
                                        <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="query">
                                            <i class="layui-icon">&#xe848;</i>
                                        </div>
                                        <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="reset">
                                            <i class="layui-icon">&#xe79b;</i>
                                        </div>
                                        <div class="layui-btn layui-btn-sm layui-btn-primary table-action action-more"
                                             shiro:hasAnyPermissions="serviceInfo:add,serviceInfo:delete,serviceInfo:export">
                                            <i class="layui-icon">&#xe875;</i>
                                        </div>
                                    </div>
                                </div>
                            </form>
                            <table lay-filter="service-provider" class="service-provider"
                                   lay-data="{id: 'service-provider'}"></table>
                        </div>

                        <div class="layui-tab-item">
                            <form class="layui-form layui-table-form apply-form" id="mine-service-apply-form" lay-filter="mine-service-apply-form"
                                  onsubmit="return false">
                                <div class="layui-row">
                                    <div class="layui-col-md10">
                                        <div class="layui-form-item">
                                            <div class="layui-inline">
                                                <label class="layui-form-label layui-form-label-sm">系统名称</label>
                                                <div class="layui-input-inline">
                                                    <input type="text" name="spName" autocomplete="on"
                                                           class="layui-input">
                                                </div>
                                            </div>

                                            <div class="layui-inline">
                                                <label class="layui-form-label layui-form-label-sm">服务名称</label>
                                                <div class="layui-input-inline">
                                                    <input type="text" name="name" autocomplete="on"
                                                           class="layui-input">
                                                </div>
                                            </div>
                                            <div class="layui-inline">
                                                <label class="layui-form-label layui-form-label-sm">接口状态</label>
                                                <div class="layui-input-inline">
                                                    <select name="status">
                                                        <option value=""></option>
                                                        <option value="0">注册</option>
                                                        <option value="1">审核</option>
                                                        <option value="2">可用</option>
                                                        <option value="3">禁用</option>
                                                    </select>
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                    <div class="layui-col-md2 layui-col-sm12 layui-col-xs12 table-action-area">
                                        <div class="layui-btn layui-btn-sm layui-btn-primary table-action"
                                             id="applied-query">
                                            <i class="layui-icon">&#xe848;</i>
                                        </div>
                                        <div class="layui-btn layui-btn-sm layui-btn-primary table-action"
                                             id="apply-reset">
                                            <i class="layui-icon">&#xe79b;</i>
                                        </div>
                                        <div class="layui-btn layui-btn-sm layui-btn-primary table-action apply-action-more"
                                             shiro:hasAnyPermissions="serviceInfo:add,serviceInfo:delete,serviceInfo:export">
                                            <i class="layui-icon">&#xe875;</i>
                                        </div>
                                    </div>
                                </div>
                            </form>
                            <table lay-filter="service-applied" class="service-applied"
                                   lay-data="{id: 'service-applied'}"></table>
                        </div>

                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="serviceInfo-status">
    {{#
    var status = {
    0: {title: '注册', color: 'cyan'},
    1: {title: '审核', color: 'blue'},
    2: {title: '可用', color: 'green'},
    3: {title: '禁用', color: 'volcano'}
    }[d.status];
    }}
    <span class="layui-badge febs-tag-{{status.color}}">{{ status.title }}</span>
</script>

<script type="text/html" id="service-applied-status">
    {{#
    var applyStatus = {
    1: {title: '待审核', color: 'blue'},
    2: {title: '同意', color: 'green'},
    3: {title: '拒绝', color: 'volcano'}
    }[d.applyStatus];
    }}
    <span class="layui-badge febs-tag-{{applyStatus.color}}">{{ applyStatus.title }}</span>
</script>

<script type="text/html" id="service-applied-option">
    <span shiro:lacksPermission="serviceInfo:view,serviceInfo:update,serviceInfo:delete">
        <span class="layui-badge-dot febs-bg-orange"></span> 无权限
    </span>
    <a lay-event="detail" shiro:hasPermission="userService:view"><i
            class="layui-icon febs-edit-area febs-green">&#xe7a5;</i></a>
    <a lay-event="edit" shiro:hasPermission="userService:update"><i
            class="layui-icon febs-edit-area febs-blue">&#xe7a4;</i></a>
    <a lay-event="del" shiro:hasPermission="userService:delete"><i
            class="layui-icon febs-edit-area febs-red">&#xe7f9;</i></a>
</script>
<script data-th-inline="none" type="text/javascript">
    layui.use(['dropdown', 'jquery', 'laydate', 'form', 'table', 'febs'], function () {


        var $ = layui.jquery,
            laydate = layui.laydate,
            febs = layui.febs,
            form = layui.form,
            table = layui.table,
            dropdown = layui.dropdown,
            $view = $('#febs-service-info'),
            $query = $view.find('#query'),
            $reset = $view.find('#reset'),
            $applyQuery = $view.find('#applied-query'),
            $applyReset = $view.find('#apply-reset'),
            $applySearchForm = $view.find('.apply-form'),
            $searchForm = $view.find('form'),
            sortObject = {field: 'spName', type: null},
            tableIns,applyTableIns;
        form.render();
        initTable();
        initApplyTable();

        dropdown.render({
            elem: $view.find('.apply-action-more'),
            click: function (name, elem, event) {
                var checkStatus = table.checkStatus('service-provider');
                if (name === 'add') {
                    febs.modal.open('添加服务', 'serviceInfo/add', {
                        btn: ['提交', '重置'],
                        area: $(window).width() <= 750 ? '95%' : '50%',
                        yes: function (index, layero) {
                            console.log(" post callback  ")
                            console.log(layero)
                            $('#esb-service-add').find('#submit').trigger('click');

                        },
                        btn2: function () {
                            $('#esb-service-add').find('#reset').trigger('click');
                            return false;
                        }
                    });
                }
                if (name === 'delete') {
                    if (!checkStatus.data.length) {
                        febs.alert.warn('请选择需要删除的服务');
                    } else {
                        febs.modal.confirm('删除用户', '确定删除该服务？', function () {
                            var serviceIds = [];
                            layui.each(checkStatus.data, function (key, item) {
                                serviceIds.push(item.userServiceId)
                            });
                            deleteUserServices(serviceIds.join(','));
                        });
                    }
                }

                if (name === 'export') {
                    var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
                    params.pageSize = $view.find(".layui-laypage-limits option:selected").val();
                    params.pageNum = $view.find(".layui-laypage-em").next().html();
                    febs.download(ctx + 'serviceInfo/excel', params, '服务信息表.xlsx');
                }
            },
            options: [{
                name: 'add',
                title: '添加服务',
                perms: 'userService:add'
            }, {
                name: 'delete',
                title: '删除服务',
                perms: 'userService:delete'
            }, {
                name: 'export',
                title: '导出Excel',
                perms: 'userService:export'
            }]
        });

//        treeSelect.render({
//            elem: $view.find('#dept'),
//            type: 'get',
//            data: ctx + 'dept/select/tree',
//            placeholder: '请选择',
//            search: false
//        });


        table.on('tool(service-provider)', function (obj) {
            var data = obj.data,
                layEvent = obj.event;
            if (layEvent === 'detail') {

                febs.navigate("/serviceInfo/detail/" + data.serviceId);
            }
            if (layEvent === 'del') {
                console.log("-- del --");
                console.log(data);
                febs.modal.confirm('删除服务', '确定删除该服务？', function () {
                    deleteUserServices(data.serviceId);
                });
            }
            if (layEvent === 'edit') {
                febs.modal.open('服务更变申请', 'userService/update/apply/' + data.serviceId, {
                    area: $(window).width() <= 750 ? '90%' : '50%',
                    btn: ['提交', '取消'],
                    yes: function (index, layero) {
//                        $('#service-info-update').find('#submit').trigger('click');
                    },
                    btn2: function () {
                        layer.closeAll();
                    }
                });
            }
        });



        table.on('sort(service-provider)', function (obj) {
            sortObject = obj;
            tableIns.reload({
                initSort: obj,
                where: $.extend(getQueryParams(), {
                    field: obj.field,
                    order: obj.type
                })
            });
        });

        $query.on('click', function () {
            var params = $.extend(getQueryParams($searchForm), {field: sortObject.field, order: sortObject.type});

            tableIns.reload({where: params, page: {curr: 1}});
        });
        $reset.on('click', function () {
            $searchForm[0].reset();
            sortObject.type = 'null';
            tableIns.reload({where: getQueryParams($searchForm), page: {curr: 1}, initSort: sortObject});
        });

        table.on('tool(service-applied)', function (obj) {
            var data = obj.data,
                layEvent = obj.event;
            if (layEvent === 'detail') {

                febs.navigate("/serviceInfo/detail/" + data.serviceId);
            }
            if (layEvent === 'del') {
                console.log("-- del --");
                console.log(data);
                febs.modal.confirm('删除申请', '确定删除该申请记录？', function () {
                    deleteUserServices(data.userServiceId);
                });
            }
            if (layEvent === 'edit') {
                febs.modal.open('服务变更申请', 'userService/update/apply' , {
                    data: {
                        userService:data
                    },
                    area: $(window).width() <= 750 ? '90%' : '50%',
                    btn: ['提交', '取消'],
                    yes: function (index, layero) {
                        $('#service-info-apply').find('#submit').trigger('click');
                    },
                    btn2: function () {
                        layer.closeAll();
                    }
                });
            }
        });

        $applyQuery.on('click', function () {
            var params = $.extend(getQueryParams($applySearchForm), {field: sortObject.field, order: sortObject.type});

            applyTableIns.reload({where: params, page: {curr: 1}});
        });

        $applyReset.on('click', function () {
            $applySearchForm[0].reset();
            sortObject.type = 'null';
            applyTableIns.reload({where: getQueryParams($applySearchForm), page: {curr: 1}, initSort: sortObject});
        });


        function initTable() {
            tableIns = febs.table.init({
                elem: $view.find('.service-provider'),
                id: 'service-provider',
//                url: ctx + 'serviceInfo/list',
                url: ctx + 'userService/list/provide/'+currentUser.username,

                cols: [[
                    {type: 'checkbox'},
                    {field: 'name', title: '服务名称', minWidth: 100},
                    {field: 'spName', title: '系统名称', minWidth: 100},
                    {field: 'description', title: '描述', minWidth: 150},
                    {field: 'version', title: '版本', minWidth: 80},
                    {field: 'protocol', title: '协议'},
                    {title: '状态', templet: '#serviceInfo-status'},
                    {field: 'principal', title: '负责人'},
                    {title: '操作', toolbar: '#service-option', minWidth: 140}
                ]]
            });
        }

        function initApplyTable() {
            applyTableIns = febs.table.init({
                elem: $view.find('.service-applied'),
                id: 'service-applied',
                url: ctx + 'userService/list/apply/'+currentUser.userId,
                cols: [[
                    {type: 'checkbox'},
                    {field: 'spName', title: '应用名称', minWidth: 100},
                    {field: 'name', title: '服务名称', minWidth: 100},
                    {title: '接口状态', templet: '#serviceInfo-status',minWidth: 80},
                    {field: 'applyTime', title: '申请时间', minWidth: 150},
                    {title: '申请状态',templet:'#service-applied-status', minWidth: 80},
                    {field: 'appKey', title: 'appKey', minWidth: 150},
                    {title: '操作', toolbar: '#service-applied-option', minWidth: 140}
                ]]
            });
        }

        function getQueryParams(selector) {
//            var createTimeFrom,
//                createTimeTo,
//                createTime = $searchForm.find('input[name="createTime"]').val();
//            if (createTime) {
//                createTimeFrom = createTime.split(' - ')[0];
//                createTimeTo = createTime.split(' - ')[1];
//            }
            return {
                name: selector.find('input[name="name"]').val().trim(),
                spName: selector.find('input[name="spName"]').val().trim(),
                status: selector.find("select[name='status']").val(),
                principal: selector.find("select[name='principal']").val(),
                invalidate_ie_cache: new Date()
            };
        }

        function deleteUserServices(userServiceIds) {
//            var currentUserId = currentUser.userId + '';
//            if (('' + userIds).split(',').indexOf(currentUserId) !== -1) {
//                febs.alert.warn('所选用户包含当前登录用户，无法删除');
//                return;
//            }
            febs.get(ctx + 'userService/delete/' + userServiceIds, null, function () {
                febs.alert.success('删除申请记录成功');
                $applyQuery.click();
            });
        }
    })
</script>